package com.net.oil.order;

import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.order.adapter.ReserveRecyclerViewAdapter;
import com.net.oil.order.bean.ReserveBeanV2;
import com.net.oil.order.contract.ReserveContract;
import com.net.oil.order.presenter.ReservePresenter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 预约activity
 */
public class ReserveActivity extends BaseActivity<ReservePresenter> implements ReserveContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.reserve_recycler_view)
    RecyclerView reserveRecyclerView;
    @BindView(R.id.no_date_tv)
    TextView noDateTv;

    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;

    private ReserveRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reserve);
        ButterKnife.bind(this);
        presenter = new ReservePresenter(this);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        showProgressDialog();
        presenter.getReserveList(page, rows);
    }

    private void initView() {

        toolbarTitle.setText("我的预约");
        toolbarGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        adapter = new ReserveRecyclerViewAdapter(this);

        reserveRecyclerView.setLayoutManager(layoutManager);
//        reserveRecyclerView.addItemDecoration(new DividerItemDecoration(this, layoutManager.getOrientation()));
        reserveRecyclerView.setAdapter(adapter);

        reserveRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //如果不能向上滚动，有更多则加载更多
                if (!reserveRecyclerView.canScrollVertically(1) && haveMore && page > 1) {
                    reserveRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter.changeMoreStatus(Constant.LOADING_MORE);
                        }
                    });
                    presenter.getReserveList(page, rows);
                }
            }
        });
    }


    /**
     * 获取预约列表成功
     */
    @Override
    public void getReserveListSuccess(ReserveBeanV2 reserveBeans) {
        dismissProgressDialog();
        if (reserveBeans != null) {

            if (reserveBeans.getList() != null && reserveBeans.getList().size() > 0) {
                if (page == 1) {
                    adapter.updateData(reserveBeans.getList());
                } else {
                    adapter.loadMore(reserveBeans.getList());
                }

                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            } else {
                if (adapter.getItemCount() == 0) {
                    noDateTv.setVisibility(View.VISIBLE);
                }
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }
        }
    }

    /**
     * 获取预约列表失败
     */
    @Override
    public void getReserveListFailed() {
        dismissProgressDialog();
    }
}
